﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iBuproModel;
using IBupro.Models;
using System.Web.Security;
using IBupro.Controllers.Classes;
using iBuproModel.Repositories.Interfaces;

namespace IBupro.Controllers
{
    /// <summary>
    /// Handles budiness logic of subjects
    /// </summary>
    [HandleError]
    public class SubjectController : CustomController
    {
        private ISubjectRepository subjects;
        

        public SubjectController()
        {
            subjects = repositoryService.SubjectRepositoryCreateInstace();
        }

        [Authorize(Roles = "Admin, Principal")]
        public ActionResult Index()
        {
            try
            {
                return View(subjects.GetAll());
            }
            catch
            {
                throw new Exception("Cannot List Subjects");
            }
            
        }

        /*
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">id of department</param>
        /// <returns></returns>
        public ActionResult Filter(int id)
        {
            try
            {
                return View(context.Subjects.Where(s => s.Department.Id == id).ToList());
            }
            catch
            {

                throw new Exception("Cannot filter projects by department");
            }
        }
        */

        public ActionResult Filter(int id,string what, string filterName)
        {
            try
            {
                IStudentPlanRepository studentPlans = repositoryService.StudentPlanRepositoryCreateInstance();
                IStudentFieldRepository studentFields = repositoryService.StudentFieldRepositoryCreateInstance();

                ViewData["filtername"] = filterName;
                switch (what)
                {
                    case "department":
                        return View(subjects.GetAll().Where(s => s.Department.Id == id).ToList());
                    case "studentplan":
                        return View(studentPlans.GetAll().Where(p => p.Id == id).SingleOrDefault().Subjects.ToList());
                    case "studentfield":
                        return View(studentFields.GetAll().Where(p => p.Id == id).SingleOrDefault().Subjects.ToList());
                    default: return RedirectToAction("Index"); 
                }
               
            }
            catch
            {

                throw new Exception("Cannot filter projects by department");
            }
        }


        [Authorize(Roles = "Admin, Principal")]
        public ActionResult Create()
        {
            try
            {
                IDepartmentRepository departments = repositoryService.DepartmentRepositoryCreateInstance();
                ViewData["Departments"] = new SelectList(departments.GetAll().Where(s => s.Visible == true).ToList(), "Id", "Name");
                return View(new SubjectModel());
            }
            catch
            {
                throw new Exception("Cannot Create Subjects");
            }
        }

        [Authorize(Roles = "Admin, Principal")]
        [HttpPost]
        public ActionResult Create(SubjectModel newSubject)
        {
            IDepartmentRepository departments = repositoryService.DepartmentRepositoryCreateInstance();
            try
            {
                Subject subject = new Subject();
                subject.Name = newSubject.Name;
                subject.Visible = true;
                subject.Shortcut = newSubject.Shortcut;
                departments.First(d => d.Id == newSubject.DepartmentId).Subjects.Add(subject);
                departments.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                throw new Exception("Cannot Create Subjects");
            }
        }

        [Authorize(Roles = "Admin, Principal")]
        public ActionResult Edit(int id)
        {
            IDepartmentRepository departments = repositoryService.DepartmentRepositoryCreateInstance();
            try
            {
                ViewData["Departments"] = new SelectList(departments.GetAll(s => s.Visible == true).ToList(), "Id", "Name");
                Subject subject = subjects.First(s => s.Id == id);
                return View(new SubjectModel(subject.Name, subject.Visible,subject.Department.Id,subject.Shortcut));
            }
            catch
            {
                throw new Exception("Cannot Edit Subject");
            }
        }

        [Authorize(Roles = "Admin, Principal")]
        [HttpPost]
        public ActionResult Edit(SubjectModel model)
        {
            IDepartmentRepository departments = repositoryService.DepartmentRepositoryCreateInstance();
            try
            {
                Subject subject = subjects.First(s => s.Id == model.Id);
                subject.Name = model.Name;
                subject.Visible = model.Visible;
                subject.Shortcut = model.Shortcut;

                if (subject.Department.Id != model.DepartmentId)
                {
                    Department dept = departments.First(d => d.Id == model.DepartmentId);
                    subject.Department = dept;
                }

                departments.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                throw new Exception("Cannot Edit Subject");
            }
        }

        public ActionResult Delete(int id)
        {
            subjects.Hide(id);
            subjects.SaveChanges();

            return RedirectToAction("Index");
        }




 
        

    }
}
